package com.bc.web.controller.admin;

import java.math.BigDecimal;
import java.util.Objects;

import com.bc.web.config.R;
import com.bc.web.model.FeeModel;
import com.fs.common.extend.FSController;
import com.fs.common.util.FSDate;
import com.fs.common.util.FSJedis;
import com.fs.common.util.FSJson;
import com.fs.common.util.FSMD5;
import com.fs.common.util.FSMoney;
import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

public class FeeModelController extends FSController {

	public void search() {
		Page<FeeModel> items = FeeModel.dao.search(super.getSearchCondition());
		setPageResultToRequest("items", items);
		render("fee/model/feeModelList.html");
	}

	public void addPage() {
		render("fee/model/feeModelAdd.html");
	}

	public void updatePage() {
		FeeModel feeModel = FeeModel.dao.findById(getPara("id"));
		setAttr("feeModel", feeModel);
		render("fee/model/feeModelUpdate.html");
	}

	@Before(Tx.class)
	public void add() {
		String minFeeStr = StrKit.isBlank(getPara("minFee")) ? "0" : getPara("minFee");
		String maxFeeStr = StrKit.isBlank(getPara("maxFee")) ? "0" : getPara("maxFee");
		FeeModel feeModel = getModel(FeeModel.class);
		StringBuffer name = new StringBuffer("基础:");
		StringBuffer md5SB = new StringBuffer();
		name.append(feeModel.getRate()).append("%");
		md5SB.append("rate").append(feeModel.getRate());
		if (StrKit.notBlank(feeModel.getFloatingRate())) {
			name.append(",浮动:").append(feeModel.getFloatingRate()).append("%");
			md5SB.append("floatingRate").append(feeModel.getFloatingRate());
		}
		BigDecimal minFee = new BigDecimal(minFeeStr).multiply(new BigDecimal("100"));
		if (minFee.intValue() > 0) {
			name.append(",最低:").append(FSMoney.changeMoneyFormat(minFeeStr)).append("元");
			md5SB.append("minFee").append(minFee.intValue());
		}
		feeModel.setMinFee(minFee.intValue());
		BigDecimal maxFee = new BigDecimal(maxFeeStr).multiply(new BigDecimal("100"));
		if (maxFee.intValue() > 0) {
			name.append(",封顶:").append(FSMoney.changeMoneyFormat(maxFeeStr)).append("元");
			md5SB.append("maxFee").append(maxFee.intValue());
		}
		feeModel.setMaxFee(maxFee.intValue());
		if (minFee.intValue() > 0 && Objects.equals(minFee.intValue(), maxFee.intValue())) {
			StringBuffer fixName = new StringBuffer();
			fixName.append("固定：").append(FSMoney.changeMoneyFormat(minFeeStr)).append("元");
			name = fixName;
		}

		String md5 = FSMD5.codeFor(md5SB.toString());
		if (isExist(md5)) {
			renderFail("已存在相同");
		} else {
			feeModel.setName(name.toString());
			feeModel.setMd5(md5);
			feeModel.setUpdateTime(FSDate.getDateTime());
			feeModel.save();
			if (R.IS_REDIS) {
				FSJedis.update(R.RK_FEE_MODEL, feeModel.getId().toString(), FSJson.toString(feeModel));
			}
			renderSuccess();
		}

	}

	@Before(Tx.class)
	public void update() {
		String minFeeStr = StrKit.isBlank(getPara("minFee")) ? "0" : getPara("minFee");
		String maxFeeStr = StrKit.isBlank(getPara("maxFee")) ? "0" : getPara("maxFee");
		FeeModel feeModel = getModel(FeeModel.class);
		StringBuffer name = new StringBuffer("基础:");
		StringBuffer md5SB = new StringBuffer();
		name.append(feeModel.getRate()).append("%");
		md5SB.append("rate").append(feeModel.getRate());
		if (StrKit.notBlank(feeModel.getFloatingRate())) {
			name.append(",浮动:").append(feeModel.getFloatingRate()).append("%");
			md5SB.append("floatingRate").append(feeModel.getFloatingRate());
		}
		BigDecimal minFee = new BigDecimal(minFeeStr).multiply(new BigDecimal("100"));
		if (minFee.intValue() > 0) {
			name.append(",最低:").append(FSMoney.changeMoneyFormat(minFeeStr)).append("元");
			md5SB.append("minFee").append(minFee.intValue());
		}
		feeModel.setMinFee(minFee.intValue());
		BigDecimal maxFee = new BigDecimal(maxFeeStr).multiply(new BigDecimal("100"));
		if (maxFee.intValue() > 0) {
			name.append(",封顶:").append(FSMoney.changeMoneyFormat(maxFeeStr)).append("元");
			md5SB.append("maxFee").append(maxFee.intValue());
		}
		feeModel.setMaxFee(maxFee.intValue());
		if (minFee.intValue() > 0 && Objects.equals(minFee.intValue(), maxFee.intValue())) {
			StringBuffer fixName = new StringBuffer();
			fixName.append("固定：").append(FSMoney.changeMoneyFormat(minFeeStr)).append("元");
			name = fixName;
		}

		String md5 = FSMD5.codeFor(md5SB.toString());
		if (isExist(md5)) {
			renderFail("已存在相同");
		} else {
			feeModel.setName(name.toString());
			feeModel.setMd5(FSMD5.codeFor(md5.toString()));
			feeModel.setUpdateTime(FSDate.getDateTime());
			feeModel.update();
			if (R.IS_REDIS) {
				FSJedis.update(R.RK_FEE_MODEL, feeModel.getId().toString(), FSJson.toString(feeModel));
			}
			renderSuccess();
		}

	}

	@Before(Tx.class)
	public void delete() {
		FeeModel f = FeeModel.dao.findById(getPara("id"));
		FeeModel.dao.deleteById(f.getId());
		if (R.IS_REDIS) {
			FSJedis.remove(R.RK_FEE_MODEL, f.getId().toString());
		}
		renderSuccess(false);
	}

	/**
	 * 检查费率模式是否存在
	 * 
	 * @param md5
	 * @return
	 */
	private boolean isExist(String md5) {
		if (FeeModel.dao.selectByMd5(md5) != null) {
			return true;
		} else {
			return false;
		}
	}
}
